#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <string.h>
#include "perso.h"

int perso(int *personnage)
{
	int num_personnage=0;
	if (*personnage==1)
	{	
		num_personnage=1;
	}
	if (*personnage==2)
	{
		num_personnage=2;
	}
	if (*personnage==3)
	{
		num_personnage=3;
	}
	if (*personnage==4)
	{
		num_personnage=4;
	}
	if (*personnage==5)
	{
		num_personnage=5;
	}
	
	 FILE* lecture_nom = NULL;
    lecture_nom = fopen("menu/personnage.txt", "w+");
    if (lecture_nom != NULL)
    {
    	fprintf(lecture_nom,"%d",num_personnage);

    fclose(lecture_nom); // On ferme le fichier qui a été ouvert

    }
    return num_personnage;
}



Animation* createAnimation(SDL_Surface* image, int width, int offset, int margin)
{
    Animation* anim = malloc(sizeof(Animation));
    anim->image = image;
    anim->width = width;
    anim->offset = offset;
    anim->margin = margin;
    anim->delay = 200;
    return anim;
}
 



void freeAnimation(Animation* anim)
{
    free(anim);
}



void setAnimationSpeed(Animation* anim, unsigned int speed)
{
    if(speed > 0)
        anim->delay = 1.f/speed*1000.f;
}
 


void drawAnimation(Animation* anim, SDL_Surface* screen, SDL_Rect* position)
{
    if(SDL_GetTicks() - anim->lasttime >= anim->delay)
    {
        anim->lasttime = SDL_GetTicks();
        anim->index++;
        if(anim->offset + anim->offset*anim->index + anim->width*anim->index >= anim->image->w)
            {
            anim->index = 0;
        }
    }
    SDL_Rect rect;
    rect.w = anim->width;
    rect.h = anim->image->h;
    rect.x = anim->offset + anim->index * anim->width + anim->index * anim->margin;
    rect.y = 0;
    SDL_BlitSurface(anim->image, &rect, screen, position);
}


